﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

/// <summary>
/// Summary description for ClassPhim
/// </summary>
public class ClassPhim
{
	public ClassPhim()
	{
		//
		// TODO: Add constructor logic here
		//
	}
    /****************************************************************************************User**********************************************************/
    DataConnection data = new DataConnection();
    //---------------------------------------------------------------------load data-----------------------------------------------------
    public DataTable DS_Phim()
    {
        string sql = "select MaPhim,HinhAnh,TenPhim,GiaThue from Phim,LoaiCD where Phim.MaLoai=LoaiCD.MaLoai";
        return data.GetData(sql);
    }
    public DataTable ChiTietPhim(string ma)
    {
        string sql = "select MaPhim,TenPhim,QuocGia,TenTL,DienVien,SoTap,HinhAnh,GioiThieu,GiaThue from Phim,LoaiCD,LoaiPhim " +
                    "where Phim.MaTL=LoaiPhim.MATL  and Phim.MaLoai=LoaiCD.MaLoai and MaPhim=@ma";
        SqlParameter pma = new SqlParameter("@ma", ma);
        return data.GetData(sql, pma);
    }
    //---------------------------------------------------------------------search data-----------------------------------------------------
    public DataTable Tim_Phim(string Ten)
    {
        string sql = "select MaPhim,HinhAnh,TenPhim,GiaThue from Phim,LoaiCD where Phim.MaLoai=LoaiCD.MaLoai and TenPhim like '%'+@Ten+'%'";
        SqlParameter pten = new SqlParameter("@Ten", Ten);
        return data.GetData(sql,pten);
    }
    public DataTable Tim_TheLoai(int TL)
    {
        string sql = "select MaPhim,Phim.MaTL,HinhAnh,TenPhim,GiaThue from Phim,LoaiCD,LoaiPhim where Phim.MaTL=LoaiPhim.MATL" +
                    " and Phim.MaLoai=LoaiCD.MaLoai and Phim.MaTL=@TL";
        SqlParameter ptl = new SqlParameter("@TL", TL);

        return data.GetData(sql, ptl);
    }
    public DataTable Tim_HSX(string HSX)
    {
        string sql = "select Phim.MaPhim,HinhAnh,TenPhim,GiaThue,QuocGia from Phim,LoaiCD where Phim.MaLoai=LoaiCD.MaLoai and QuocGia=@HSX";
        SqlParameter phsx = new SqlParameter("@HSX", HSX);

        return data.GetData(sql, phsx);
    }
    /******************************************************************************************Admin*****************************************************************/
    //---------------------------------------------------------------------load data-----------------------------------------------------
    public DataTable DanhSachCD()
    {
        string sql = "select MaPhim,TenPhim,Phim.MaLoai,TenLoai,Phim.MaTL,TenTL,DienVien,SoTap,SoBanSao,HinhAnh,GioiThieu,NamSX,QuocGia " +
                        "from Phim,LoaiCD,LoaiPhim where Phim.MaTL=LoaiPhim.MATL and Phim.MaLoai=LoaiCD.MaLoai";
        return data.GetData(sql);
    }

    public DataTable TheLoaiPhim()
    {
        string sql = "select * from LoaiPhim";
        return data.GetData(sql);
    }
    //---------------------------------------------------------------------delete_insert_updata data-----------------------------------------------------
    public DataTable XoaPhim(int ma)
    {
        string xoa = "delete from Phim where MaPhim=@ma";
        SqlParameter pxoa = new SqlParameter("@ma", ma);
        return data.GetData(xoa, pxoa);
    }
    public DataTable ThemCD(string tenphim, int Lcd, int LPhim, string dv, int tap, int bansao, string ha, string gt, int nsx,string qg)
    {
        SqlParameter ptencd = new SqlParameter("@TenCD", tenphim);
        SqlParameter plcd = new SqlParameter("@MaLoai", Lcd);
        SqlParameter plphim = new SqlParameter("@MaTL", LPhim);
        SqlParameter pdv = new SqlParameter("@DienVien", dv);
        SqlParameter ptap = new SqlParameter("@SoTap", tap);
        SqlParameter pbs = new SqlParameter("@SoBanSao", bansao);
        SqlParameter pha = new SqlParameter("@HinhAnh", ha);
        SqlParameter pgt = new SqlParameter("@GioiThieu", gt);
        SqlParameter pnsx = new SqlParameter("@NamSX", nsx);
        SqlParameter pqg = new SqlParameter("@QuocGia", qg);

        string sql = "insert into Phim values(@TenCD,@MaLoai,@MaTL,@DienVien,@SoTap,@SoBanSao,@HinhAnh,@GioiThieu,@NamSX,@QuocGia)";
        return data.GetData(sql, ptencd, plcd, plphim, pdv, ptap, pbs, pha, pgt, pnsx,pqg);
    }
    public DataTable SuaCD(int mCD, string TenCD, string Lcd, string LPhim, string dv, int tap, int bansao, string ha, string gt, string nsx,string qg)
    {
        SqlParameter pcd = new SqlParameter("@cd", mCD);
        SqlParameter ptencd = new SqlParameter("@tencd", TenCD);
        SqlParameter plcd = new SqlParameter("@lcd", Lcd);
        SqlParameter plphim = new SqlParameter("@lphim", LPhim);
        SqlParameter pdv = new SqlParameter("@dv", dv);
        SqlParameter ptap = new SqlParameter("@tap", tap);
        SqlParameter pbs = new SqlParameter("@bs", bansao);
        SqlParameter pha = new SqlParameter("@ha", ha);
        SqlParameter pgt = new SqlParameter("@gt", gt);
        SqlParameter pnsx = new SqlParameter("@nsx",nsx);
        SqlParameter pqg = new SqlParameter("@qg", qg);

        string sql = "update Phim set TenPhim=@tencd,MaLoai=@lcd,MaTL=@lphim,DienVien=@dv,SoTap=@tap,SoBanSao=@bs,HinhAnh=@ha,GioiThieu=@gt,NamSX=@nsx,QuocGia=@qg where MaPhim=@cd";
        return data.GetData(sql, pcd, ptencd, plcd, plphim, pdv, ptap, pbs, pha, pgt, pnsx,pqg);
    }
    //---------------------------------------------------------------------search data-----------------------------------------------------
    public DataTable TimPhim(string ten,int lp,string qg, string dv)
    {

        string sql = "select Phim.MaPhim,TenPhim,Phim.MaLoai,TenLoai,Phim.MaTL,TenTL,DienVien,SoTap,SoBanSao,HinhAnh,GioiThieu,NamSX,QuocGia"
                            + " from Phim,LoaiCD,LoaiPhim where Phim.MaTL=LoaiPhim.MATL"
                            + " and Phim.MaLoai=LoaiCD.MaLoai and 1=1";
        if (ten != null)
            sql = sql + " and TenPhim like '%'+@Phim+'%'";
        if (lp !=int.Parse("0"))
            sql = sql + " and Phim.MaTL =@Theloai";
        if (qg != null)
            sql = sql + " and QuocGia like '%'+@Quocgia+'%'";
        if (dv != null)
            sql = sql + " and DienVien like '%'+@Dienvien+'%'";

        SqlParameter p = new SqlParameter("@Phim", ten);
        SqlParameter plp = new SqlParameter("@Theloai", lp);
        SqlParameter pqg = new SqlParameter("@Quocgia", qg);
        SqlParameter pdv = new SqlParameter("@Dienvien", dv);

        return data.GetData(sql, p, plp, pqg, pdv);
    }
    public DataTable TheLoaiCD()
    {
        string sql = "select MaLoai,TenLoai from LoaiCD";
        return data.GetData(sql);
    }
    public DataTable DanhSachHSX()
    {
        string sql = "select QuocGia from Phim";
        return data.GetData(sql);
    }
    public DataTable ThemThueCD(int tp,int sm,float gt)
    {
        string sql = "insert into ThueCD(MaPhim,SoMuon,GiaThue) values(@MaPhim,@SoMuon,@GiaThue)";
        SqlParameter pt = new SqlParameter("@TenPhim", tp);
        SqlParameter ps = new SqlParameter("@SoMuon", sm);
        SqlParameter pg = new SqlParameter("@GiaThue", gt);
        return data.GetData(sql,pt,ps,pg);
    }


}
